package com.dzq.study.translate.serialize;


import com.dzq.study.translate.annos.SwitchDic;
import com.dzq.study.translate.annos.SwitchEnum;
import com.dzq.study.translate.annos.SwitchMap;
import com.dzq.study.translate.interfaces.BaseDicSwitchService;
import com.dzq.study.translate.interfaces.BaseEnumSwitchService;
import com.dzq.study.translate.utils.SpringBeanUtil;
import com.dzq.study.translate.utils.SwitchMapUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class TranslateSerialize extends JsonSerializer<Object> implements ContextualSerializer {

    private Class clazz;

    private String target;


    public TranslateSerialize() {
    }

    public TranslateSerialize(Class clazz) {
        this.clazz = clazz;
    }

    public TranslateSerialize(String target) {
        this.target = target;
    }

    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        try {
            //枚举实现
            if (clazz!=null&&clazz.isEnum()){
                Method method = clazz.getMethod("values");
                Object invoke = method.invoke(null);
                BaseEnumSwitchService[] services = (BaseEnumSwitchService[]) invoke;
                String value = services[0].getMappingValueByKey(Objects.isNull(o) ? "" : o.toString());
                jsonGenerator.writeString(value);
                return;
            }
            //map实现
            if (StringUtils.isNotBlank(target)){
                Map<String, String> map = SwitchMapUtil.getMap(target);
                if (map!=null){
                    String value = map.get(o);
                    jsonGenerator.writeString(StringUtils.isNotBlank(value)?value:"");
                }else {
                    jsonGenerator.writeString("");
                }
            }
            if (clazz!=null){
                Class[] interfaces = clazz.getInterfaces();
                long count = Arrays.stream(interfaces).filter(item -> item.equals(BaseDicSwitchService.class)).count();
                if (count>0){
                    BaseDicSwitchService service = (BaseDicSwitchService) SpringBeanUtil.getBean(clazz);
                    jsonGenerator.writeString(service.switchDic(Objects.isNull(o) ? "" : o.toString()));
                }
            }
        }catch (Exception e){
            log.error("****************** translate error ***************************");
            e.printStackTrace();
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        // 为空直接跳过
        if (property == null) {
            return prov.findNullValueSerializer(null);
        }
        SwitchEnum switchEnum = property.getAnnotation(SwitchEnum.class);
        if (switchEnum != null) {
            return new TranslateSerialize(switchEnum.enumClass());
        }
        SwitchMap switchMap = property.getAnnotation(SwitchMap.class);
        if (switchMap != null) {
            return new TranslateSerialize(switchMap.target());
        }
        SwitchDic switchDic = property.getAnnotation(SwitchDic.class);
        if (switchDic != null) {
            return new TranslateSerialize(switchDic.translateClass());
        }
        return prov.findValueSerializer(property.getType(), property);
    }
}
